package com.linewow.xhyy.nihaonew.view;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cunoraz.gifview.library.GifView;
import com.linewow.xhyy.nihaonew.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by LXR on 2016/9/8.
 *
 * 太乱了 一定要修改  有长进吗
 *
 */
public class VoiceButton2Red extends RelativeLayout implements View.OnClickListener{
    @Bind(R.id.view_voice_button_audio_time)
    TextView viewVoiceButtonAudioTime;
    @Bind(R.id.view_voice_button_audio_wave)
    ImageView viewVoiceButtonAudioWave;
    @Bind(R.id.view_voice_button_audio_wave1)
    GifView viewVoiceButtonAudioWave1;
    private LayoutInflater layoutInflater;
    private String path;

    private String TAG = "VoiceButton2";
    private boolean playingFlag;
    private MediaPlayer mediaPlayer;
    private boolean urlFlag;
    private String urlLength;
    private String urlPath;

    public VoiceButton2Red(Context context) {
        super(context);
        init(context, null);
    }

    public VoiceButton2Red(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    public VoiceButton2Red(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.view_widget_voice_button, this);
        ButterKnife.bind(this);
        VoiceButton2Red.this.setOnClickListener(VoiceButton2Red.this);
    }

    public void setPath(String path) {
        this.path = path;
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stop();

                }
            });
            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                    stop();
                    return false;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        double size = mediaPlayer.getDuration();
        String timelong1 = (int) Math.ceil((size / 1000)) + "";
        viewVoiceButtonAudioTime.setText(timelong1);//只是本地的文件

    }


    public void start() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
            try {
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stop();
                    }
                });
                mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                        stop();
                        return false;
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mediaPlayer.start();
        playingFlag = true;
        updateView();

    }

    private void updateView() {
        if(playingFlag){
            viewVoiceButtonAudioWave1.setVisibility(VISIBLE);
            viewVoiceButtonAudioWave.setVisibility(GONE);
        }else{
            viewVoiceButtonAudioWave1.setVisibility(GONE);
            viewVoiceButtonAudioWave.setVisibility(VISIBLE);
        }
    }

    public void stop() {
        playingFlag = false;
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        updateView();
    }

    @Override
    public void onClick(View view) {


        if(urlFlag){
            Log.e(TAG,"将要下载了");
            OkHttpUtils.get()
                    .url(urlPath)
                    .build()
                    .execute(new FileCallBack( Environment.getExternalStorageDirectory().getAbsolutePath() + "/NiHao/","download.m4a") {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(File response, int id) {
                            Log.e(TAG,"是否存在"+response.exists());
                            path=response.getAbsolutePath();//setPath只是显示了长度 略过
                            start();
                            urlFlag=false;
                        }
                    });
        }else{

            if(this.path==null){
                return;
            }

            if (playingFlag) {
                stop();
            } else {
                start();
            }

        }

    }

    public void setUrlPath(String length,String urlPath){
        this.urlLength=length;
        this.urlPath=urlPath;
        Log.e(TAG,"长度"+urlLength);
        Log.e(TAG,"路径"+urlPath);
        urlFlag=true;
        viewVoiceButtonAudioTime.setText(length);
    }
}
